//
//  MessageBoxBaseView.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-16.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#define animateTime1 0.2f //动画切换时间


#import "MessageBoxBaseView.h"
#import "Common.h"
#import "Singleton.h"
#import "FileManager.h"
#import "Imports1.h"

@interface MessageBoxBaseView(Private)
- (void) add1fingerTap;
@end

@implementation MessageBoxBaseView
@synthesize contentFrame;

- (id)initWithContentViewController:(UIViewController *)contentViewController{
    MyNSLog(@"in MessageBoxBaseView initWithContentViewController...");
    SysMsg = [[NSString alloc] initWithFormat:@"SysMsg_MessageBoxBaseView_%@", contentViewController.nibName];
    MyNSLog(@"add observer SysMsg = %@",SysMsg);
    
    self = [super initWithFrame:CGRectMake(0, 0, 1024, 768)];//全屏背景大小
    if (self) {       
        //全屏的半透明背景图
        UIImageView *mask = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        mask.image = [FileManager getImage:@"mask_bkg.png" type:3 withRect:mask.frame]; 
        [self addSubview:mask];
        [mask release];
        
        _contentViewController = contentViewController;
        CGSize size = CGSizeMake(_contentViewController.view.frame.size.width, _contentViewController.view.frame.size.height);
        CGPoint pos = CGPointMake((1024-size.width)/2, (768-size.height)/2);
        _contentFrame = CGRectMake(pos.x, pos.y, size.width, size.height);
        [_contentViewController.view setFrame:_contentFrame];
        [self addSubview:_contentViewController.view];
        [self setBackgroundColor:[UIColor clearColor]];
        
        [self add1fingerTap];
        [self fadeIn:animateTime1 delegate:self startSelector:@selector(startAnimate) stopSelector:@selector(stopAnimate)];
        addNObserver(@selector(viewWillDisappear), SysMsg);
    }
    return self;
}
-(void) startAnimate{
    AppSession.isAnimating = YES;
}
-(void) stopAnimate{
    AppSession.isAnimating = NO;
}
-(void) setContentFrame:(CGRect)cf{
    _contentFrame = cf;
    [_contentViewController.view setFrame:_contentFrame];
}

#pragma mark - GestureRecognizer
- (void) add1fingerTap
{
	UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    recognizer.numberOfTouchesRequired = 1;//多少跟手指
    recognizer.numberOfTapsRequired = 1;//点击几次
    recognizer.delegate = self;
	[self addGestureRecognizer:recognizer];
	[recognizer release];	
}
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
	CGPoint pointLoction = [recognizer locationInView:self];
    if (![Common checkPointInRange:pointLoction Range:_contentFrame]) {
        MyNSLog(@"单击有效界面以外！退出弹出窗口MessageBoxBaseView...");
        postN(SysMsg);
    }
}
-(void) viewWillDisappear{
    AppSession.isPopOutFullScreen = NO;
    [self removeFromSuperview];
//    [self fadeOut:0.0f delegate:self startSelector:@selector(startAnimate) stopSelector:@selector(viewDidMoveOut)];
}
-(void) viewDidMoveOut{
    [self removeFromSuperview];
}

-(void)dealloc{
    MyNSLog(@"MessageBoxBaseView is deallocing...");
    MyNSLog(@"remove observer SysMsg = %@",SysMsg);
    removeNObserver(SysMsg);
    [SysMsg release];
    SysMsg = nil;
    [_contentViewController release];
    [super dealloc];
}
@end
